package com.qipay.mapper;

import com.qipay.baas.model.CpWithdrawTradlog;
import com.qipay.order.config.OrderQueryEnum;
import com.qipay.withdraw.config.WithdrawOperationEnum;
import com.qipay.withdraw.config.WithdrawQueryEnum;
import com.qipay.withdraw.config.WithdrawStateEnum;
import com.qipay.withdraw.config.WithdrawTypeEnum;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CpWithdrawTradlogSqlProvider extends com.qipay.baas.mapper.CpWithdrawTradlogSqlProvider {


    //统计信息
    public String findSubbranch(String province, String city, String bank) {
        String selectsql = "select * from banks where province like '%"+province+"%' "
                +"and city like '%"+city+"%' and bank_name like '%"+bank+"%'";

        return selectsql;
    }

    public String countApply() {
        return "select count(0) from cp_withdraw_tradlog where 1=1 and hasRemind is null;";
    }
    public String count(Long cpId, Byte state, String from, String to) {

        String selectsql = "select count(0) from cp_withdraw_tradlog where 1=1 %s %s %s %s; ";
        String selectstatesql = "select count(0) from cp_withdraw_tradlog where   ( state = 1 or  state = 3 or state = 4 )  %s  %s %s ;";
        String cpIdsql = cpId != null ? String.format(" and cp_id= %s ", cpId) : "";
        String statesql =    state != null && state != -1 ? String.format(" and state= %s ", state) : "" ;
        String fromdsql = StringUtils.isNotBlank(from) ? String.format(" and create_time>= '%s 00:00:00' ", from) : "";
        String tosql = StringUtils.isNotBlank(to) ? String.format(" and create_time <= '%s 23:59:59' ", to) : "";
        if(state!=null&&state==-2){
            return  String.format(selectstatesql,cpIdsql,fromdsql,tosql);
        }

        return String.format(selectsql,cpIdsql,statesql,fromdsql,tosql);

    }

    //统计信息
    public String countInfo(Long cpId, Byte state, String from, String to) {
        String selectsql = "select sum(amount) as  amountCount,sum(fee) as  feeCount , sum(actual_amount)  as  actualAmountCount " +
                " from cp_withdraw_tradlog where 1=1 %s %s %s %s; ";
        String selectstatesql = "select sum(amount) as  amountCount,sum(fee) as  feeCount , sum(actual_amount)  as  actualAmountCount " +
                " from cp_withdraw_tradlog where   ( state = 1 or  state = 3 or state = 4 )  %s %s %s ;";
        String cpIdsql = cpId != null ? String.format(" and cp_id= %s ", cpId) : "";
        String statesql =    state != null && state != -1 ? String.format(" and state= %s ", state) : "" ;
        String fromdsql = StringUtils.isNotBlank(from) ? String.format(" and create_time>= '%s 00:00:00' ", from) : "";
        String tosql = StringUtils.isNotBlank(to) ? String.format(" and create_time <= '%s 23:59:59' ", to) : "";
        if(state!=null&&state==-2){
            return  String.format(selectstatesql,cpIdsql,fromdsql,tosql);
        }

        return String.format(selectsql,cpIdsql,statesql,fromdsql,tosql);
    }

    public String list(Long cpId, Byte state, String from, String to, Integer p, Integer pz) {
        String selectsql = "select * from cp_withdraw_tradlog where 1=1 %s %s %s %s  order by state asc, create_time desc limit %s, %s; ";
        String selectstatesql = "select * from cp_withdraw_tradlog where   ( state = 1 or  state = 3 or state = 4 )  %s  %s %s  order by state asc, create_time desc limit %s, %s;";
        String cpIdsql = cpId != null ? String.format(" and cp_id= %s ", cpId) : "";
        String statesql =    state != null && state != -1 ? String.format(" and state= %s ", state) : "" ;
        String fromdsql = StringUtils.isNotBlank(from) ? String.format(" and create_time>= '%s 00:00:00' ", from) : "";
        String tosql = StringUtils.isNotBlank(to) ? String.format(" and create_time <= '%s 23:59:59' ", to) : "";
        if(state!=null&&state==-2){
            return  String.format(selectstatesql,cpIdsql,fromdsql,tosql,p,pz);
        }

        return String.format(selectsql,cpIdsql,statesql,fromdsql,tosql,p,pz);

    }

    public String listByCpId(Long cpId, Byte state) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());

        String selectsql = "select * from cp_withdraw_tradlog where 1=1 %s %s %s";
        String cpIdsql = cpId != null ? String.format(" and cp_id= %s ", cpId) : "";
        String statesql = state != null && state != -1 ? String.format(" and state= %s ", state) : "" ;
        String datesql = String.format(" and finish_time>= '%s 00:00:00' ",date );

        return String.format(selectsql,cpIdsql,statesql,datesql);
    }

    public String listByTime(String from, String to) {
        return String.format("select * from cp_withdraw_tradlog where 1=1 %s %s order by state asc, create_time ",
                StringUtils.isNotBlank(from) ? String.format(" and create_time>= '%s 00:00:00' ", from) : "",
                StringUtils.isNotBlank(to) ? String.format(" and create_time <= '%s 23:59:59' ", to) : "");
    }

    public String listByState(Byte state) {
        return String.format("select id,cp_id,amount,type from cp_withdraw_tradlog where 1=1 and state = %s;",state);
    }
    public String select(CpWithdrawTradlog cpWithdrawTradlog){
        return "select * from cp_withdraw_tradlog";
    }
}